﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using Spider.Rules;
using Spider.Entitys;

namespace Spider.Analyse
{
    public class 片区解析规则 : AnalyseBase, IAnalyse
    {
        public override List<T> GetListData<T>(HtmlDocument doc, IRule rule, object o = null)
        {
            List<T> listEntity = new List<T>();
            //更加xpath获取总的对象，如果不为空，就继续选择dl标签
            HtmlNode res = doc.DocumentNode.SelectSingleNode(rule.RuleXPathString());
            if (res != null)
            {
                var list = res.SelectNodes(@"tr");//选择标签数组
                foreach (var item in list)
                {
                    var tdlist = item.SelectNodes(@"td");
                    var td0 = tdlist[0];
                    var td1 = tdlist[1];
                    var td2 = tdlist[2];
                    if (td2.InnerText.Equals("名称"))
                    {
                        continue;
                    }
                    int ClassifyCode = int.Parse(td1.InnerText);
                    string GB11643_1999 = td0.InnerText;
                    AreaEntity entity = new AreaEntity()
                    {
                        AreaName = td2.InnerText,
                        ClassifyCode = ClassifyCode,
                        GB11643_1999 = GB11643_1999,
                        Level = 5,
                    };
                    listEntity.Add((T)(object)entity);
                }
            }
            return listEntity;
        }
    }
}
